package com.cwt_taxi_xa;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.cwt_taxi_xa.service.DvrService;
import com.cwt_taxi_xa.service.FloatService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;
import com.cwt_taxi_xa.widget.llx.CustomCircle;
import com.cwt_taxi_xa.widget.llx.CustomCircle.OnMyClickListener;
import com.cwt_taxi_xa.widget.llx.RoundMenuView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class VideoActivity extends CWTBaseActivity implements OnClickListener {

	private SurfaceView mSurfaceView;
	SurfaceHolder sholder;
	Camera camera;
	Camera.Parameters parameters;

	public int chl = 701;

	// 按照协议中按键顺序对应--触发时下标即键值
	int[] btids = { R.id.button10, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9, R.id.button18, R.id.button12, R.id.button19, R.id.button14,
			R.id.button20, R.id.button16, R.id.button11, R.id.button17, R.id.button13, R.id.button15, R.id.button21 };
	Button[] bts = new Button[btids.length];

	int touchx;
	int touchy;
	int dvrx;
	int dvry;
	int viewx;
	int viewy;
	
	ImageView iv1, iv2, iv3;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// if (msg.what == 1001) {// 刷新时间
			// String time = (String) msg.obj;
			// tv_time.setText(time);
			// }
			// else if (msg.what == 1002) {//用于切换tvin通道
			// chl = 702;
			// parameters.setPreviewSize(chl, 501);
			// camera.setParameters(parameters);
			// }
			if (msg.what == 1003) {
				try {
					stopView();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			// else if (msg.what == 1007) {
			// // 刷新右上角状态位-网络
			// iv_online
			// .setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs1_on
			// : R.drawable.cwtic_bs1_off);
			// } else if (msg.what == 1008) {
			// // 刷新右上角状态位-平台
			// iv_center
			// .setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs2_on
			// : R.drawable.cwtic_bs2_off);
			// } else if (msg.what == 1009) {
			// // 刷新右上角状态位-定位
			// iv_gps
			// .setImageResource((Boolean) msg.obj ? R.drawable.cwtic_bs3_on
			// : R.drawable.cwtic_bs3_off);
			// }
			


		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_video);

		// tv_time = (TextView) findViewById(R.id.textView1);
		// iv_retrun = (ImageView) findViewById(R.id.imageView7);
		// iv_online = (ImageView) findViewById(R.id.imageView8);
		// iv_center = (ImageView) findViewById(R.id.imageView9);
		// iv_gps = (ImageView) findViewById(R.id.imageView10);

		for (int i = 0; i < bts.length; i++) {
			bts[i] = (Button) findViewById(btids[i]);
			bts[i].setTag(i);
			bts[i].setOnClickListener(this);
		}

		mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);

		mSurfaceView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				touchx = (int) event.getRawX();
				touchy = (int) event.getRawY();
				AppLog.e("testtouch------:  " + touchx + "  ,  " + touchy);

				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					AppLog.e("testtouch------:ACTION_UP");
					break;
				case MotionEvent.ACTION_MOVE:
					AppLog.e("testtouch------:ACTION_MOVE");
					break;
				case MotionEvent.ACTION_DOWN:
					try {
						AppLog.e("testtouch------:ACTION_DOWN");
						dvrx = (((2900*10000)/viewx)*touchx)/10000;
						dvry = (((3095*10000)/viewy)*touchy)/10000;
						AppLog.e("testtouch------:  " + dvrx + "  ,  " + dvry);
						DvrService.getInstance().onTouch(dvrx, dvry);
					} catch (Exception e) {
					}
					break;
				case MotionEvent.ACTION_OUTSIDE:
					AppLog.e("testtouch------:ACTION_OUTSIDE");
					break;
				case MotionEvent.ACTION_CANCEL:
					AppLog.e("testtouch------:ACTION_CANCEL");
					break;
				}

				return false;
			}
		});

		mSurfaceView.getHolder().addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub

				AppLog.e("---------------------surfaceCreated");
				try {
					sholder = holder;

					// 友浩系统的奇葩设定。。。
					camera = Camera.open(3);

					parameters = camera.getParameters();

					// 获取预览的各种分辨率
					// List<Size> supportedPreviewSizes =
					// parameters.getSupportedPreviewSizes();
					// for(Size s: supportedPreviewSizes){
					// AppLog.e(s.width+"-----------"+s.height);
					// }
					/**
					 * this.camera = Camera.open(3); this.param =
					 * this.camera.getParameters();
					 * this.param.setPreviewSize(this.width, this.height);
					 * this.param.setPreviewFpsRange(4, 10);
					 * this.param.setPictureFormat(256);
					 * this.param.set("jpeg-quality", 95);
					 * this.param.setPictureSize(1600, 900);
					 * this.camera.setParameters(this.param);
					 */

					// 友浩系统的奇葩设定。。。
					parameters.setPreviewSize(chl, 501);
					// parameters.setPreviewSize(720, 576);
					// parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
					camera.setParameters(parameters);
					camera.setPreviewDisplay(sholder);
					camera.startPreview();
				} catch (Exception e) {
					e.printStackTrace();
				}

				//获取到实际的触摸范围，再点击时根据dvr要求换算
				mSurfaceView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						// TODO Auto-generated method stub
						mSurfaceView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
						viewx = mSurfaceView.getMeasuredWidth();
						viewy = mSurfaceView.getMeasuredHeight();
					}
				});

			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				AppLog.e("---------------------surfaceChanged");
				sholder = holder;
				// try {
				// camera.setPreviewDisplay(sholder);
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				// camera.startPreview();
				// try {
				// camera.stopPreview();
				// camera.setPreviewDisplay(holder);
				// camera.startPreview();
				// } catch (Exception e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
			}
		});
		
		
		
		RoundMenuView roundMenuView = (RoundMenuView) findViewById(R.id.dvrcc);
		RoundMenuView.RoundMenu roundMenu = new RoundMenuView.RoundMenu();
        roundMenu.selectSolidColor = 0x66338fcc;
        roundMenu.strokeColor = 0x66ffffff;
        roundMenu.icon= BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_dvr_yk_right1);
        roundMenu.onClickListener=new OnClickListener() {
            @Override
            public void onClick(View view) {
            	DvrService.getInstance().onClick(17);
            }
        };
        roundMenuView.addRoundMenu(roundMenu);
        
        
        roundMenu = new RoundMenuView.RoundMenu();
        roundMenu.selectSolidColor = 0x66338fcc;
        roundMenu.strokeColor = 0x66ffffff;
        roundMenu.icon= BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_dvr_yk_right1);
        roundMenu.onClickListener=new OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		DvrService.getInstance().onClick(18);
        	}
        };
        roundMenuView.addRoundMenu(roundMenu);
        
        
        roundMenu = new RoundMenuView.RoundMenu();
        roundMenu.selectSolidColor = 0x66338fcc;
        roundMenu.strokeColor = 0x66ffffff;
        roundMenu.icon= BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_dvr_yk_right1);
        roundMenu.onClickListener=new OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		DvrService.getInstance().onClick(16);
        	}
        };
        roundMenuView.addRoundMenu(roundMenu);


        roundMenu = new RoundMenuView.RoundMenu();
        roundMenu.selectSolidColor = 0x66338fcc;
        roundMenu.strokeColor = 0x66ffffff;
        roundMenu.icon= BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_dvr_yk_right1);
        roundMenu.onClickListener=new OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		DvrService.getInstance().onClick(19);
        	}
        };
        roundMenuView.addRoundMenu(roundMenu);
 
 

 
        roundMenuView.setCoreMenu(0x00000000,
        						  0x66338fcc, 
        						  0x66ffffff, 
                1, 0.45,
                BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_dvr_yk_ok1), 
                new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    	DvrService.getInstance().onClick(13);
                    }
                });

		
		
		iv1 = (ImageView) findViewById(R.id.imageView1);
		iv2 = (ImageView) findViewById(R.id.imageView2);
		iv3 = (ImageView) findViewById(R.id.imageView3);
		iv1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				DvrService.getInstance().onClick(15);
			}
		});
		iv2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				DvrService.getInstance().onClick(14);
			}
		});
		iv3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				stopView();
			}
		});

	}

	void stopView() {
		if(camera!=null){
			camera.stopPreview();
			camera.release();
			camera = null;
		}
		finish();
	}



	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		try {
			stopView();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub
		switch (index) {
		case 1:
			handler.sendMessage(handler.obtainMessage(1007, status));
			break;
		case 2:
			handler.sendMessage(handler.obtainMessage(1008, status));
			break;
		case 3:
			handler.sendMessage(handler.obtainMessage(1009, status));
			break;
		default:
			break;
		}
	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		if (type == 1002) {
			handler.sendMessage(handler.obtainMessage(1001, obj));
		}
		if (type == 1014) {
			handler.sendEmptyMessage(1003);
		}
	}

	
	@Override
	public void onClick(View arg0) {
		int key = (Integer) arg0.getTag();// 获得键值
		if (key == 20) {
			stopView();
		}else {
			DvrService.getInstance().onClick(key);
		}
	}

}
